package org.osami.commons.smarthome.programnotifier;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;


public class Activator implements BundleActivator {

	private static BundleContext bundleContext = null;
	ProgramDatabase db;
	
	Timer timer;
	Calendar currentTime = Calendar.getInstance();
	
	public static BundleContext getBundleContext() {
		return bundleContext;
	}

	class fireTimeout extends TimerTask {
		public void run() {
			System.out.println("ProgramDatabase Activator: fireTimeout running");
			//ProgramDatabase db = ProgramDatabase.getInstance();
			db = new ProgramDatabase();
			db.reload();
    		db.print();
			}
		}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		bundleContext = context;

		timer = new Timer();
		timer.schedule(new fireTimeout(), 0, 24*60*60*1000);
		System.out.println("ProgramDatabase Activator: Timer scheduled");
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		System.out.println("ProgramDatabase Activator: Goodbye World!!");
        timer.cancel(); //Terminate the timer thread
		Runtime.getRuntime().gc();
	}

}
